package com.css.bjdt_report.report.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_report.report.entity.RptGroup;
import com.css.bjdt_report.report.entity.RptTemplate;
import com.css.bjdt_report.util.CommonUtil;

@Service
public class ReportGroupManageService {
	@Autowired
	private Dao dao;

	@Autowired
	private ReportTemplateManageService reportTemplateManageService;

	/**
	 * 新增套表
	 * @param record
	 * @return
	 */
	public RptGroup insertRptGroup(RptGroup rptGroup) {
		if (StringUtils.isEmpty(rptGroup.getGroupId())) {
			rptGroup.setGroupId(CommonUtil.getUUID());
		}
		return dao.insert(rptGroup);
	}

	/**
	 * 根据id查询相关套表信息
	 * @param id
	 * @return
	 */
	public RptGroup queryByID(String id) {
		return dao.fetch(RptGroup.class, id);
	}

	/**
	 * 更新套表
	 * @param rptGroup
	 * @return
	 */
	public int update(RptGroup rptGroup) {
		return dao.updateIgnoreNull(rptGroup);
	}

	/**
	 * 删除套表
	 * @param groupId
	 * @return
	 */
	public void deleteGroup(String groupId, String annualId) {
		//先删子表
		List<RptTemplate> rptTemplateList = dao.query(RptTemplate.class, Cnd.where("GROUPID", "=", groupId));
		for (RptTemplate rptTemplate : rptTemplateList) {
			reportTemplateManageService.deleteAnRptTemplate(rptTemplate.getTemplateId(), annualId);
		}
		//再删套表
		dao.clear(RptGroup.class, Cnd.where("GROUPID", "=", groupId));
	}

	/**
	 * 查询所有套标
	 * @return
	 */
	public List<RptGroup> queryRptGroupList(String annualId) {
		return dao.query(RptGroup.class, Cnd.where("ANNUALID", "=", annualId));
	}
}
